package com.ysm.smfu.activity;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.bean.ZhangDanDetail;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.RefreshLayout;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AccountDetailActivity extends BaseRefreshActivity<ZhangDanDetail.ZhangDan> {

    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.listView_dingdanDetail)
    ListView listViewDingdanDetail;
    @BindView(R.id.refresh)
    RefreshLayout refresh;
    @BindView(R.id.isNot)
    TextView isNot;
    private DingDanAdapter adapter;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_ding_dan_detail);
        ButterKnife.bind(this);
        initRefresh(refresh, true);
        titleBody.setText("账户明细");
        mainActivityTopbarImg.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new DingDanAdapter();
        listViewDingdanDetail.setAdapter(adapter);
        getDingDanDetail();
    }


    @Override
    public void load(boolean isLoad) {
        // 上拉加载
        if (isLoad)
            page++;
        getDingDanDetail();
    }

    /**
     * 获取订单详情
     */
    public void getDingDanDetail() {
        BaseProtocal.builder(false).getAccountDetail(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("页码：" + page +"账单详情：" ,response);
                String state = JsonUtil.getState(response);
                if (TextUtils.equals(state, "1")) {
                    ZhangDanDetail zhangDanDetail = new Gson().fromJson(response, ZhangDanDetail.class);
                    List<ZhangDanDetail.ZhangDan> currBillList = zhangDanDetail.billList;

                    setLoadData(currBillList);
                    adapter.notifyDataSetChanged();
                    noData(true, response);
                } else {
                    noData(false, response);
                }
            }

            @Override
            public void onFail(Exception e) {
                noData(false, null);
            }
        }, page);
    }

    public void noData(boolean isTrue, String response) {
        if (!isTrue)
            onRefreshError();
        else if (datas.size() == 0) {
            isNot.setText(JsonUtil.getMessage(response));
            isNot.setTextColor(getResources().getColor(R.color.gray_normal));
            isNot.setVisibility(View.VISIBLE);
            refresh.setVisibility(View.GONE);
        } else {
            isNot.setVisibility(View.GONE);
            refresh.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 提示对话框--没有点击事件
     *
     * @param activity
     * @param msg
     */
    public void showErrorDialog(FragmentActivity activity, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(activity).setMessage(msg)
                .setNegativeButton("确定", null).create();
        dialog.show();
    }

    //           red         red                              red      red                      red        red               red
// 01:消费  02:充值  03:消费佣金 04:转出佣金 06:提现 07:返现  08:系统赠送  09:系统扣除  10:代理赠送  11:返还商户  12:活动
    String[] types = {"01", "02", "03", "04", "06", "07", "08", "09", "10", "11", "12"};
    String[] types_text = {"消费", "充值", "消费佣金", "转出佣金", "提现", "返现", "系统赠送", "系统扣除", "代理赠送", "返还商户", "活动"};
    int[] colors = {R.color.textColor_bonus, R.color.red, R.color.red, R.color.textColor_bonus, R.color.textColor_bonus, R.color.red, R.color.red, R.color.textColor_bonus,
            R.color.red, R.color.red, R.color.red};

    private class DingDanAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            为奇数时显示（一次消费,两条信息）
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = UIUtils.inflate(R.layout.holder_accountdetail);
                viewHolder = new ViewHolder(convertView);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
//            返回积分
            ZhangDanDetail.ZhangDan jifen = datas.get(position);
//                返还积分
            viewHolder.dingdanTime.setText(jifen.createTime);
            if (TextUtils.equals(jifen.moneyStatus, "01")) {
                String data = "";
//                if (!TextUtils.isEmpty(jifen.points) && !TextUtils.equals(jifen.points, "0") && !TextUtils.isEmpty(jifen.money) && !TextUtils.equals(jifen.money, "0.00")) {
//                    data = "+" + jifen.money + "现金" + "+" + jifen.points + "兑换券";
//                } else if (!TextUtils.isEmpty(jifen.money) && !TextUtils.equals(jifen.money, "0") && !TextUtils.equals(jifen.money, "0.00"))
//                    data = jifen.money + "现金";
//                else if (!TextUtils.isEmpty(jifen.points) && !TextUtils.equals(jifen.points, "0")) {
//                    data = jifen.points + "兑换券";
//                }


                if (!TextUtils.isEmpty(jifen.money) && !TextUtils.equals(jifen.money, "0.00"))
                    data = "+" + jifen.money + "元";
                if (!TextUtils.isEmpty(jifen.points) && !TextUtils.equals(jifen.points, "0"))
                    data = data + "+" + jifen.points + "兑换券";
                if (!TextUtils.isEmpty(jifen.integration) && !TextUtils.equals(jifen.integration, "0") && !TextUtils.equals(jifen.integration, "0.00"))
                    data = data + "+" + jifen.integration + "积分";
                if (!TextUtils.isEmpty(jifen.voucher) && !TextUtils.equals(jifen.voucher, "0") && !TextUtils.equals(jifen.voucher, "0.00"))
                    data = data + "+" + jifen.voucher + "代金券";

                if (!TextUtils.isEmpty(data)) {
                    viewHolder.moneyDetail.setText(data);
                    viewHolder.account_all.setVisibility(View.VISIBLE);
                } else
                    viewHolder.account_all.setVisibility(View.GONE);
            } else if (TextUtils.equals(jifen.moneyStatus, "02")) {
                String data = "";

                if (!TextUtils.isEmpty(jifen.money) && !TextUtils.equals(jifen.money, "0.00"))
                    data = "-" + jifen.money + "元";
                if (!TextUtils.isEmpty(jifen.points) && !TextUtils.equals(jifen.points, "0"))
                    data = data + "-" + jifen.points + "兑换券";
                if (!TextUtils.isEmpty(jifen.integration) && !TextUtils.equals(jifen.integration, "0") && !TextUtils.equals(jifen.integration, "0.00"))
                    data = data + "-" + jifen.integration + "积分";
                if (!TextUtils.isEmpty(jifen.voucher) && !TextUtils.equals(jifen.voucher, "0") && !TextUtils.equals(jifen.voucher, "0.00"))
                    data = data + "-" + jifen.voucher + "代金券";

                if (!TextUtils.isEmpty(data)) {
                    viewHolder.moneyDetail.setText(data);
                    viewHolder.account_all.setVisibility(View.VISIBLE);
                } else
                    viewHolder.account_all.setVisibility(View.GONE);
            }
            for (int i = 0; i < types.length; i++) {
                if (TextUtils.equals(jifen.type, types[i])) {
                    viewHolder.dingdanType.setText(types_text[i]);
                    viewHolder.dingdanType.setTextColor(getResources().getColor(colors[i]));
                }
            }
            return convertView;
        }
    }

    public class ViewHolder {

        public AutoLinearLayout account_all;
        public TextView dingdanTime;
        public TextView moneyDetail;
        public TextView dingdanType;

        public ViewHolder(View view) {
            dingdanTime = (TextView) view.findViewById(R.id.dingdan_time);
            moneyDetail = (TextView) view.findViewById(R.id.moneyDetail);
            dingdanType = (TextView) view.findViewById(R.id.dingdanType);
            account_all = (AutoLinearLayout) view.findViewById(R.id.account_all);
            view.setTag(this);
        }
    }
}
